﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.DataModel;
using YArchitech.LIB;

namespace HYElectricSetting
{
	public partial class FittingSelect : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

		public FittingSelect(Autodesk.Revit.DB.Document doc, string title, string tableName, int category, int subCategory, List<string> familyNames)
		{
			this.InitializeComponent();
			this.doc = doc;
			this.Text = title;
			this.tableName = tableName;
			this.category = category;
			this.subCategory = subCategory;
			this.familyNames = familyNames;
		}

		private void FittingSelect_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.GetFamilyDatas();
			this.FilllvwFamilyNameInfo();
			this.FilllvwImageInfo();
			bool flag = this.lsbFittingName.Items.Count > 0;
			if (flag)
			{
				this.lsbFittingName.SelectedIndex = 0;
			}
		}

		public void GetFamilyDatas()
		{
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			List<ListItem> allName = spaceFamilyDataReader.GetAllName(this.tableName);
			foreach (ListItem listItem in allName)
			{
				int id = Convert.ToInt32(listItem.Tag);
				FittingSelectData fittingSelectData = new FittingSelectData();
				List<ListItem> infoById = spaceFamilyDataReader.GetInfoById(this.tableName, id);
				foreach (ListItem item in infoById)
				{
					CableTrayFittingDataInfo.SetValueForFamilyObject(fittingSelectData, item);
				}
				bool flag = fittingSelectData.Category != 0;
				if (flag)
				{
					bool flag2 = fittingSelectData.Category == this.category && fittingSelectData.SubCategory == this.subCategory;
					if (flag2)
					{
						this.fittingInfos.Add(fittingSelectData);
					}
				}
			}
		}

		private void FilllvwFamilyNameInfo()
		{
			foreach (FittingSelectData fittingSelectData in this.fittingInfos)
			{
				this.lsbFittingName.Items.Add(fittingSelectData.Name);
			}
		}

		private void FilllvwImageInfo()
		{
			foreach (FittingSelectData fittingSelectData in this.fittingInfos)
			{
				Image image = Image.FromFile(fittingSelectData.Image);
				this.lvwFittingImage.LargeImageList.Images.Add(fittingSelectData.Name, image);
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Name = fittingSelectData.Name;
				listViewItem.Text = fittingSelectData.Name;
				listViewItem.Tag = fittingSelectData;
				listViewItem.ImageKey = fittingSelectData.Name;
				this.lvwFittingImage.Items.Add(listViewItem);
			}
			bool flag = this.lvwFittingImage.Items.Count > 0;
			if (flag)
			{
				FittingSelect.SendMessage(this.lvwFittingImage.Handle, this.LVM_SETICONSPACING, 0, 7864418);
			}
		}

		private void lsbFittingName_SelectedIndexChanged(object sender, EventArgs e)
		{
			string key = this.lsbFittingName.SelectedItem.ToString();
			this.lvwFittingImage.Items[key].Selected = true;
		}

		private void btnLoad_Click(object sender, EventArgs e)
		{
			bool flag = this.lvwFittingImage.SelectedItems.Count > 0;
			if (flag)
			{
				foreach (object obj in this.lvwFittingImage.SelectedItems)
				{
					ListViewItem listViewItem = (ListViewItem)obj;
					try
					{
						string item = this.familyName = Path.GetFileNameWithoutExtension(((FittingSelectData)listViewItem.Tag).Family);
						bool flag2 = !this.familyNames.Contains(item);
						if (flag2)
						{
							Transaction transaction = new Transaction(this.doc, "SaveFittingData");
							transaction.Start();
							Family family;
							bool flag3 = this.doc.LoadFamily(((FittingSelectData)listViewItem.Tag).Family, new MyFamilyLoadOptions(), out family);
							transaction.Commit();
							bool flag4 = family != null;
							if (flag4)
							{
								this.family = family;
							}
						}
						this.familyName = item;
						break;
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message);
						break;
					}
				}
				base.DialogResult = DialogResult.OK;
			}
		}

		private int LVM_SETICONSPACING = 4149;

		private List<FittingSelectData> fittingInfos = new List<FittingSelectData>();

		private Document doc = null;

		private string tableName = "";

		private int category = 0;

		private int subCategory = 0;

		public string familyName = null;

		public Family family = null;

		private List<string> familyNames = new List<string>();
	}
}
